<template>
	<view>
		<view class="page-head" :style="{paddingTop: menuButtonTop, height: menuButtonHeight, color: color, background: background}" :class="{fixed: fixed}">
			<view class="head-hd" @click="pageBack" v-if="isBack">
				<image class="backimg" src="../../static/back.png" mode="" v-if="mode == 'light'"></image>
				<image class="backimg" src="../../static/back2.png" mode="" v-if="mode == 'dark'"></image>
			</view>
			<view class="left-title" v-if="leftTitle">{{leftTitle}}</view>
			<view class="head-bd" :style="{height: menuButtonHeight, lineHeight: menuButtonHeight}">{{title}}</view>
		</view>
		<view :style="{height: placeHeight}"></view>
	</view>
</template>

<script>
	export default {
		name:"navbar",
		props: {
			// 是否固定在顶部
			fixed: {
				type: Boolean,
				default: true
			},
			// 一级页面不要返回按钮
			isBack: {
				type: Boolean,
				default: true
			},
			mode: {
				type: String,
				default: 'light'
			},
			// 导航栏标题
			title: {
				type: String,
				default: ''
			},
			leftTitle: {
				type: String,
				default: ''
			},
			color: {
				type: String,
				default: '#333'
			},
			background: {
				type: String,
				default: '#fff'
			}
		},
		data() {
			return {
				menuButtonTop: uni.getMenuButtonBoundingClientRect().top + 'px',
				menuButtonHeight: uni.getMenuButtonBoundingClientRect().height + 'px',
				placeHeight: uni.getMenuButtonBoundingClientRect().top + uni.getMenuButtonBoundingClientRect().height + 10 + 'px'
			};
		},
		methods: {
			pageBack() {
				uni.navigateBack({
					fail: () => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-head{
		position: relative;
		z-index: 999;
		left: 0;
		right: 0;
		padding: 0 30rpx 20rpx;
		display: flex;
		align-items: center;
		font-size: 36rpx;
		&.fixed{
			position: fixed;
		}
		.head-hd{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 48rpx;
			height: 100%;
			.backimg{
				width: 48rpx;
				height: 48rpx;
			}
		}
		.left-title{
			font-size: 32rpx;
			font-weight: bold;
		}
		.head-bd{
			position: absolute;
			left: 140rpx;
			right: 140rpx;
			min-width: 0;
			text-align: center;
		}
	}
</style>